package com.nexus.core.auth;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.nexus.core.sys.dto.SysOrgDTO;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;

/**
 * 用户登录信息
 *
 * @Date 2025/7/28 16:40
 * @Author luzhengning
 **/
@Data
public class LoginUser implements UserDetails {

    /**
     * 用户ID
     *
     * @Date 2025/7/30 15:25
     * @Author luzhengning
     **/
    private String id;
    /**
     * 登录账号
     *
     * @Date 2025/7/30 15:50
     * @Author luzhengning
     **/
    @NotNull(message = "用户名不能为空")
    private String username;
    /**
     * 用户密码
     *
     * @Date 2025/7/31 11:07
     * @Author luzhengning
     **/
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY) // 仅允许写入（接收参数）
    @NotNull(message = "密码不能为空")
    private String password;
    /**
     * 用户昵称
     *
     * @Date 2025/8/12 14:15
     * @Author luzhengning
     **/
    private String nickname;
    /**
     * 用户头像
     *
     * @Date 2025/8/12 14:16
     * @Author luzhengning
     **/
    private String avatar;
    /**
     * 用户信息
     *
     * @Date 2025/7/28 16:41
     * @Author luzhengning
     **/
    private Object user;
    /**
     * 用户类型，用于推断user用户对象的类型
     *
     * @Date 2025/7/28 16:41
     * @Author luzhengning
     **/
    private LoginUserType userType;
    /**
     * 用户Token
     *
     * @Date 2025/7/30 15:34
     * @Author luzhengning
     **/
    private String accessToken;
    /**
     * 刷新token
     *
     * @Date 2025/8/12 14:10
     * @Author luzhengning
     **/
    private String refreshToken;
    /**
     * 登录时间
     *
     * @Date 2025/7/30 15:34
     * @Author luzhengning
     **/
    private LocalDateTime loginDate;
    /**
     * Token过期时间
     *
     * @Date 2025/8/1 15:36
     * @Author luzhengning
     **/
    private LocalDateTime expires;
    /**
     * 客户端类型
     *
     * @Date 2023/6/19 11:02
     * @Author luzhengning
     **/
    private String clientType;
    /**
     * 角色列表
     *
     * @Date 2025/8/12 14:11
     * @Author luzhengning
     **/
    private List<String> roles;
    /**
     * 权限列表
     *
     * @Date 2025/8/12 14:11
     * @Author luzhengning
     **/
    private List<String> permissions;

    /**
     * 当前组织机构
     * @Date 2025/8/12 14:21
     * @Author luzhengning
     **/
    private SysOrgDTO currentOrg;

    /**
     * 组织机构
     *
     * @Date 2025/8/12 14:20
     * @Author luzhengning
     **/
    private List<SysOrgDTO> orgs;

    public LoginUser() {
    }

    public LoginUser(Object user) {
        this.user = user;
    }

    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY) // 仅允许写入（接收参数）
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return List.of();
    }
}
